package com.fastCms.business.frame.busi.impl;

import com.alibaba.fastjson.JSON;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.frame.constant.RedisConst;
import com.fastCms.business.frame.enums.UserTypeEnum;
import com.fastCms.business.frame.busi.RedisCloudUserModelManager;
import com.fastCms.business.frame.pojo.CloudUserModel;
import com.fastCms.business.frame.utils.CommonsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Description token管理
 * @Param
 * @Return
 * @Date 2019/3/12 下午2:11 lsquan
 */
@Component
public class RedisCloudUserModelManagerImpl implements RedisCloudUserModelManager {

    @Autowired
    private StringRedisTemplate redis;

    @Autowired
    public void setRedis(StringRedisTemplate redis) {
        this.redis = redis;
        //泛型设置成Long后必须更改对应的序列化方案
//        redis.setKeySerializer(new JdkSerializationRedisSerializer());
    }

    @Override
    public CloudUserModel createToken(UserTypeEnum userType, String userId) {
        // key = 1_USER_ID:1 -> token
        String userIdKey = userType.getType() + "_" + RedisConst.USER.USER_ID + userId;
        String redisToken = redis.boundValueOps(userIdKey).get();
        if (StringUtils.isNotBlank(redisToken)) {
            return new CloudUserModel(userId, redisToken);
        }
        String newToken = CommonsUtil.getUUID();
        // key = 1_TOKEN:abcdefg    -> userModel
//        String tokenKey = userType.getType() + "_" + RedisConst.USER.TOKEN + newToken;
        CloudUserModel userModel = new CloudUserModel(userId, newToken);

//        //存储到redis并设置过期时间 token->userMode
//        redis.boundValueOps(tokenKey).set(JSON.toJSONString(userModel), CommonConst.USER_MANAGER.TOKEN_TIME_OUT, TimeUnit.HOURS);
//        //避免重复创建 TOKEN_ACCOUNT_ID20188 ->token
//        redis.boundValueOps(userIdKey).set(newToken, CommonConst.USER_MANAGER.TOKEN_TIME_OUT, TimeUnit.HOURS);
        return userModel;
    }

    @Override
    public void saveRedisToken(UserTypeEnum userType, CloudUserModel userModel) {
        // key = 1_TOKEN:abcdefg    -> userModel
        String tokenKey = userType.getType() + "_" + RedisConst.CLOUD_USER.CLOUD_TOKEN + userModel.getToken();
        String userIdKey = userType.getType() + "_" + RedisConst.CLOUD_USER.CLOUD_USER_ID + userModel.getUserId();
        //避免重复创建 TOKEN_ACCOUNT_ID20188 ->token
        redis.boundValueOps(userIdKey).set(userModel.getToken(), CommonConst.USER_MANAGER.TOKEN_TIME_OUT, TimeUnit.HOURS);
        //存储到redis并设置过期时间 token->userMode
        redis.boundValueOps(tokenKey).set(JSON.toJSONString(userModel), CommonConst.USER_MANAGER.TOKEN_TIME_OUT, TimeUnit.HOURS);

    }


    @Override
    public void deleteToken(UserTypeEnum userType, String userId) {
        String getTokenByUserId = userType.getType() + "_" + RedisConst.USER.USER_ID + userId;
        redis.delete(getTokenByUserId);
    }

    /**
     * @Description 校验token
     * @Param [model]
     * @Return boolean
     * @Date 2019/3/22 下午6:29 lsquan
     */
    @Override
    public boolean checkToken(CloudUserModel model) {
        if (model == null) {
            return false;
        }
        String token = redis.boundValueOps(String.valueOf(model.getUserId())).get();
        if (token == null || !token.equals(model.getToken())) {
            return false;
        }
        //如果验证成功，说明此用户进行了一次有效操作，延长token的过期时间
        redis.boundValueOps(token).expire(CommonConst.USER_MANAGER.TOKEN_TIME_OUT, TimeUnit.HOURS);
        return true;
    }


}
